{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Assignment 5: Data Wrangling and Computational Complexity"},{"metadata":{},"cell_type":"markdown","source":"## Exercise 1: Manipulating data frames (20 points)"},{"metadata":{},"cell_type":"markdown","source":"List of data frames is a common output of some data collection tools. Find below a list of 10 data frames. Append them together by row. Keep only unique rows"},{"metadata":{"trusted":true},"cell_type":"code","source":"set.seed(2021)\ndfs <- lapply(1:10, function(n) data.frame(x = letters[sample(1:26,20,TRUE)], y = sample(1:26,20,TRUE)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Exercise 1:\ndf_1 <- dfs[[1]]\ndf_2 <- dfs[[2]]\ndf_3 <- dfs[[3]]\ndf_4 <- dfs[[4]]\ndf_5 <- dfs[[5]]\ndf_6 <- dfs[[6]]\ndf_7 <- dfs[[7]]\ndf_8 <- dfs[[8]]\ndf_9 <- dfs[[9]]\ndf_10 <- dfs[[10]]\n\nlibrary(dplyr)\nlibrary(magrittr)\n\nnewdf <- rbind(df_1,df_2,df_3,df_4,df_5,df_6,df_7,df_8,df_9,df_10) %>% distinct()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Exercise 2: Pivoting tables (20 points)"},{"metadata":{},"cell_type":"markdown","source":"Let's use __[Kaggle 2020 Machine Learning and Data Science Survey](https://www.kaggle.com/c/kaggle-survey-2020/)__, that we encountered before. In question 7 respondents were asked \"What programming languages do you use on a regular basis? (Select all that apply)\". Calculate percentages of respondents who use each of the programming languages. Sort them by popularity.<br>\nTip: You can use tidyr and dplyr packages to pivot subset of columns into longer format.\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"URL <- \"https://raw.githubusercontent.com/ASDS-TCD/POP77001_Computer_Programming_2021/main/data/kaggle_survey_2020_responses.csv\"\n\n# As the header of this dataset is composite (conissting ot 2 rows)\n# we start by reading in the first 2 rows and then using the header\n# of that 'header' dataset for the actual full dataset\ncol_names <- names(readr::read_csv(URL, n_max = 2))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"kaggle2020 <- readr::read_csv(URL, col_names = col_names, skip = 2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Exercise 2:\nlibrary(tidyr)\nlibrary(dplyr)\n\nkaggle2020_pivoted <- kaggle2020 %>% \n  pivot_longer(cols = starts_with(\"Q7_\"),\n               names_to= \"Questions\",\n               values_to = \"Programming_Languanges\",\n               values_drop_na=T\n               )\n\nlibrary(summarytools)\nfreq(kaggle2020_pivoted$Programming_Languanges, order = \"freq\", cumul = F)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Exercise 3: Order of growth (20 points)"},{"metadata":{},"cell_type":"markdown","source":"If some function has the following number of steps, what is its order of growth? Write Big-O notation for each of the cases"},{"metadata":{},"cell_type":"markdown","source":"- $5n + n^2 + 3$\n- $3n(\\log n) + n^5 + 2n!$\n- $4n^2 + 3n^3 + 2n^4$\n- $\\log n + n \\log n$\n- $3 + 4$"},{"metadata":{},"cell_type":"markdown","source":"<b>Exercise 3:</b> <br>\n<ol>\n    <li>$5n + n^2 + 3$ <br> <b>Notation:-</b> $O(n^2)$ </li>\n    <li>$3n(\\log n) + n^5 + 2n!$ <br> <b>Notation:-</b> $O(n!)$ </li>\n    <li>$4n^2 + 3n^3 + 2n^4$ <br> <b>Notation:-</b> $O(n^4)$ </li>\n    <li>$\\log n + n \\log n$ <br> <b>Notation:-</b> $O(\\log n)$ </li>\n    <li> $3 + 4$ <br> <b>Notation:-</b> $O(1)$</li>\n"},{"metadata":{},"cell_type":"markdown","source":"# Exercise 4: Analysis of time complexity (20 points)"},{"metadata":{},"cell_type":"markdown","source":"Consider two functions below in R and Python. What is their order of growth (big-O)? Explain your reasoning in a few sentences."},{"metadata":{"trusted":true},"cell_type":"code","source":"# Calculate sum and product of vector elements\nsum_product <- function(v) {\n  summ <- 0\n  product <- 1\n  for (i in seq_along(v)) {\n    summ <- summ + v[i]\n  }\n  for (j in seq_along(v)) {\n    product <- product * v[j]\n  }\n  res <- list(sum = summ, product = product)\n  return(res)\n}\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def sum_digits(n):\n    '''Takes positive integer n and sums its digits.'''\n    res = 0\n    while n > 0:\n        res += int(n % 10)\n        n = int(n / 10)\n    return res\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<b>Exercise 4:</b> <br>\nIn this case, we two for loops, which means the first loop will execute the sum n times and the second loop will also execute the product n times. So we will add the time complexities and the order of the growth is $O(n)$. It is a linear time complexity. The time to execute the algorithm is directly proportional to the input size."},{"metadata":{},"cell_type":"markdown","source":"# Exercise 5: Calculating t-tests (20 points)"},{"metadata":{},"cell_type":"markdown","source":"__[Student's t-test](https://en.wikipedia.org/wiki/Student's_t-test)__ (developed by __[William Gosset](https://en.wikipedia.org/wiki/William_Sealy_Gosset)__ when working at the Guinness Brewery) is a classical tool for testing differences between groups when samples are small.<br>\nAssume that you drew a random sample of 200 individuals and ran 1,000 experiments on them (an experiment does not need to mean something big, think, different shapes of some button on a website or Guinness served at slightly different temperatures).\nImplement a function that takes a matrix of experimental results and a vector with experimental/control-group assignment and returns a vector of calculated t-statistics for each of the experiments. See simulated input data below.<br>\nBenchmark the time it takes for the function to run on the specified input.<br>\nTip: Internally, you can use built-in `t.test()` function in R.\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"set.seed(2021)\nn <- 200\nm <- 1000\nmat <- matrix(rnorm(m * n, mean = 20, sd = 3), nrow = 200)\n# For simplicity, let's assume that assignment to control and experimental groups is always the same\ngrp <- rep(0:1, times = 100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Exercise 5:\nt.vect <- vector(length = m)\nfor (i in 1:m) {\n# Creating new variables for the sample sizes\n  boot.c <- sample(mat,size = n,replace = T)\n  boot.p <- sample(grp, size = n, replace = T)\n# A command to compute the t- statistic for the created sample sizes\n  ttest <- t.test(boot.c, boot.p)\n  #storing the t- statistics in a vector\n  t.vect[i] <- ttest$statistic\n}\n# A command to print the first 10 components of the vector","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"3.6.3"}},"nbformat":4,"nbformat_minor":5}